﻿// Author: Jithu Jose, Email: jithu89@hotmail.it
// 
// This file is part of Schedule.
//
// Schedule is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Schedule is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Schedule.  If not, see <http://www.gnu.org/licenses/>.

namespace JJware.Schedule
{
    using System.Drawing;
    using System.Windows.Forms;

    #region Enumerations

    public enum DividerOrientation
    {
        Vertical, Horizontal
    }

    #endregion Enumerations

    public partial class Divider : UserControl
    {
        #region Fields

        Color _Color1;
        Color _Color2;
        DividerOrientation _Orientation;

        #endregion Fields

        #region Constructors

        public Divider()
        {
            InitializeComponent();
            _Orientation = DividerOrientation.Horizontal;
        }

        #endregion Constructors

        #region Properties

        public Color Color1
        {
            get { return _Color1; }
            set { _Color1 = value; }
        }

        public Color Color2
        {
            get { return _Color2; }
            set { _Color2 = value; }
        }

        public DividerOrientation Orientation
        {
            get { return _Orientation; }
            set { _Orientation = value; }
        }

        #endregion Properties

        #region Methods

        private void Divider_Paint(object sender, PaintEventArgs e)
        {
            DrawDivider(e.Graphics);
        }

        private void DrawDivider(Graphics graphics)
        {
            Point point1 = new Point();
            Point point2 = new Point();
            int valueToAdd = 0;
            if (_Orientation == DividerOrientation.Horizontal)
            {
                if (Width < Height) {
                    int value1 = Width;
                    int value2 = Height;
                    Swap(ref value1, ref value2);
                    Width = value1;
                    Height = value2;
                }
                point1 = new Point(0, Height / 2);
                point2 = new Point(Width, Height / 2);
                valueToAdd = 1;
            }
            else if (_Orientation == DividerOrientation.Vertical)
            {
                if (Height < Width)
                {
                    int value1 = Width;
                    int value2 = Height;
                    Swap(ref value1, ref value2);
                    Width = value1;
                    Height = value2;
                }
                point1 = new Point(Width / 2, 0);
                point2 = new Point(Width / 2, Height);
                valueToAdd = -1;
            }
            graphics.DrawLine(new Pen(new SolidBrush(_Color1)),
                   point1, point2);
            if (valueToAdd == 1)
            {
                point1.Y += valueToAdd;
                point2.Y += valueToAdd;
            }
            else if (valueToAdd == -1)
            {
                point1.X += valueToAdd;
                point2.X += valueToAdd;
            }
            graphics.DrawLine(new Pen(new SolidBrush(_Color2)), point1, point2);
        }

        private void Swap(ref int value1, ref int value2)
        {
            int appo = value1;
            value1 = value2;
            value2 = appo;
        }

        #endregion Methods
    }
}